package com.sec.etech.presale.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sec.etech.common.JsonUtil;
import com.sec.etech.presale.mapper.OmsListMapper;
import com.sec.etech.presale.mapper.OmsTemplateMapper;
import com.sec.etech.presale.model.OmsDto;
import com.sec.etech.presale.model.OmsList;
import com.sec.etech.presale.model.OmsTemplate;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class OmsService {
    @Resource
    OmsListMapper omsListMapper;
    @Resource
    OmsTemplateMapper omsTemplateMapper;



    public JSONObject translateJsonById(Long id, String templateKey){
        OmsList omsList = omsListMapper.selectById(id);

        OmsTemplate omsTemplate = omsTemplateMapper.selectOne(new QueryWrapper<OmsTemplate>()
                .lambda().eq(OmsTemplate::getTemplateKey, templateKey));

        return translateJson(
                JSON.parseObject(omsList.getAttachment1()) ,
                JSONObject.parseObject(omsTemplate.getKeyMap()),
                null
        );
    }

    public JSONObject translateJson(JSONObject data, JSONObject keyMap, JSONObject valueMap){
        JSONObject newData = new JSONObject();
        for (String key: data.keySet()){
            String translateKey = keyMap.getString(key);
            if (StringUtils.isEmpty(translateKey)){
                newData.put(key, data.get(key));
            }else{
                newData.put(translateKey, data.get(key));
            }
        }

        return newData;
    }


    public void createByApi(OmsDto dto){
        log.info("OMS create {}", dto);

        OmsList omsList = new OmsList();
        omsList.setNum(dto.getNum());
        omsList.setUniqueid(dto.getUniqueid());
        omsList.setAttachment1(JSONObject.toJSONString(dto.getData()));

        omsList.setPlatform("SWP");
        omsList.setCategory("cmpCreated");


        int count = omsListMapper.selectCount(new QueryWrapper<OmsList>()
                .lambda()
                .eq(OmsList::getUniqueid, dto.getUniqueid())
                .eq(OmsList::getNum, dto.getNum())

        );

        if (count == 0){
            omsList.setDealType("add");
        }else{

            List<OmsList> list = omsListMapper.selectList(new QueryWrapper<OmsList>()
                            .lambda()
                            .eq(OmsList::getUniqueid, dto.getUniqueid())
                            .eq(OmsList::getNum, dto.getNum())
                    .orderByDesc(OmsList::getPostdate).last("limit 1"));

            JsonUtil.JsonDiff jsonDiff = JsonUtil.compareJson(JSON.parseObject(list.get(0).getAttachment1()), dto.getData());

            JSONObject obj = new JSONObject(jsonDiff.dataDiffDesc);
            omsList.setAttachment2(JSONObject.toJSONString(obj));


            omsList.setDealType("edit");
        }

        omsList.setPostdate(LocalDateTime.now());

        omsListMapper.insert(omsList);



    }

    public static void main(String[] args) {

        String a = "{\"项目名称\":\"TS04\",\"交货日期\":\"2024-05-15\",\"质保金(万元)\":\"19\",\"项目属性\":\"【技服】常规\",\"税率（%）\":\"13\",\"合同类型\":\"技服\",\"销售订单号\":\"DXDEE\",\"报价单/投标文件\":\"http://10.9.70.202:8856/fileProxy/getFileById?id=1793501223175049217\",\"项目主管\":\"何俊\",\"项目执行部门\":\"【STP】服务支持处\",\"销售经理\":\"何俊\",\"正式合同\":\"http://10.9.70.202:8856/fileProxy/getFileById?id=1793505496080306178\",\"交货地点\":\"D\",\"合同方口径\":\"【电站集团】服务公司\",\"合同签订日期\":\"2024-05-08\",\"是否需要技术准备\":\"是\",\"执行经理\":\"何俊\",\"质保金比例(%)\":\"5\",\"排产日期\":\"2024-05-08\",\"合同交货日期\":\"2024-05-14\",\"机组台数\":\"1\",\"责任部门\":\"STP设计处\",\"地址\":\"广东省阳江市阳西县溪头镇\",\"合同编号\":\"SCEED\",\"是否发起待确认品牌编制流程\":\"是\",\"机组物料号\":\"CDEEE\",\"实际完工日期\":\"2024-05-15\",\"业主（电厂）名称\":\"阳西海滨电力发展有限公司\",\"销售部门\":\"【工厂】服务销售处\",\"不含税销售价(万元)\":\"17.4\",\"联系人手机\":\"X\",\"原机组信息\":\"B191-13-25\",\"无价格版合同\":\"http://10.9.70.202:8856/fileProxy/getFileById?id=1793505512144490497\",\"甲方合同编号\":\"cdeddd\",\"制造单号\":\"24-5101-001\",\"预估含税合同金额（元）\":\"1002365.0\",\"所属集团\":\"广东珠江投资管理集团有限公司\",\"联系人姓名\":\"S\",\"联系人职务\":\"D\",\"合同付款方式\":\"xde\",\"省市区\":\"中国,广东,阳江\",\"承接方式\":\"转接\",\"OEM\":\"是\",\"合同甲方\":\"张家港沙洲电力有限公司\",\"项目类别\":\"火电\",\"合同签署人\":\"ssdsc\",\"技术协议\":\"http://10.9.70.202:8856/fileProxy/getFileById?id=1793501240614965250\",\"含税合同金额（万元）\":\"12\"}";
        String b = "{\"项目名称\":\"TS05\",\"交货日期\":\"2024-05-18\",\"质保金(万元)\":\"19\",\"项目属性\":\"【技服】常规\",\"税率（%）\":\"13\",\"合同类型\":\"技服\",\"销售订单号\":\"DXDEE\",\"报价单/投标文件\":\"http://10.9.70.202:8856/fileProxy/getFileById?id=1793501223175049217\",\"项目主管\":\"何俊\",\"项目执行部门\":\"【STP】服务支持处\",\"销售经理\":\"何俊\",\"正式合同\":\"http://10.9.70.202:8856/fileProxy/getFileById?id=1793505496080306178\",\"交货地点\":\"D\",\"合同方口径\":\"【电站集团】服务公司\",\"合同签订日期\":\"2024-05-08\",\"是否需要技术准备\":\"是\",\"执行经理\":\"何俊\",\"质保金比例(%)\":\"5\",\"排产日期\":\"2024-05-08\",\"合同交货日期\":\"2024-05-14\",\"机组台数\":\"1\",\"责任部门\":\"STP设计处\",\"地址\":\"广东省阳江市阳西县溪头镇\",\"合同编号\":\"SCEED\",\"是否发起待确认品牌编制流程\":\"是\",\"机组物料号\":\"CDEEE\",\"实际完工日期\":\"2024-05-15\",\"业主（电厂）名称\":\"阳西海滨电力发展有限公司\",\"销售部门\":\"【工厂】服务销售处\",\"不含税销售价(万元)\":\"17.4\",\"联系人手机\":\"X\",\"原机组信息\":\"B191-13-25\",\"无价格版合同\":\"http://10.9.70.202:8856/fileProxy/getFileById?id=1793505512144490497\",\"甲方合同编号\":\"cdeddd\",\"制造单号\":\"24-5101-001\",\"预估含税合同金额（元）\":\"1002365.0\",\"所属集团\":\"广东珠江投资管理集团有限公司\",\"联系人姓名\":\"S\",\"联系人职务\":\"D\",\"合同付款方式\":\"xde\",\"省市区\":\"中国,广东,阳江\",\"承接方式\":\"转接\",\"OEM\":\"是\",\"合同甲方\":\"张家港沙洲电力有限公司\",\"项目类别\":\"火电\",\"合同签署人\":\"ssdsc\",\"技术协议\":\"http://10.9.70.202:8856/fileProxy/getFileById?id=1793501240614965250\",\"含税合同金额（万元）\":\"12\"}";

        JsonUtil.JsonDiff jsonDiff = JsonUtil.compareJson(JSON.parseObject(a), JSON.parseObject(b));
        System.out.println(jsonDiff);
    }

}
